package com.example.controller;

import com.example.common.RequirePermission;
import com.example.common.Result;
import com.example.common.PageResult;
import com.example.dto.RechargeDTO;
import com.example.dto.RechargeRecordQueryDTO;
import com.example.entity.User;
import com.example.service.AdminService;
import com.example.service.RechargeService;
import com.example.service.UserService;
import com.example.util.JwtUserUtil;
import com.example.vo.RechargeRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/admin/recharge")
@CrossOrigin
public class AdminRechargeController {
    
    @Autowired
    private RechargeService rechargeService;
    
    @Autowired
    private JwtUserUtil jwtUserUtil;
    
    /**
     * 管理员为用户充值
     */
    @PostMapping("")
    @RequirePermission("admin:recharge")
    public Result<String> adminRecharge(@RequestBody RechargeDTO rechargeDTO) {
        // 获取当前管理员信息
        Long adminUserId = jwtUserUtil.getCurrentUserIdFromContext();
        String adminUsername = jwtUserUtil.getCurrentUsernameFromContext();
        
        if (adminUserId == null || adminUsername == null) {
            return Result.error("管理员未登录");
        }
        
        return rechargeService.adminRecharge(adminUserId, adminUsername, rechargeDTO);
    }
    
    /**
     * 分页查询充值记录（管理员查看所有用户）
     */
    @GetMapping("/records")
    @RequirePermission("admin:recharge:records")
    public Result<PageResult<RechargeRecordVO>> getRechargeRecords(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) Double minAmount,
            @RequestParam(required = false) Double maxAmount,
            @RequestParam(required = false) String rechargeType,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) String orderNo,
            @RequestParam(required = false) String operatorName) {
        
        RechargeRecordQueryDTO queryDTO = new RechargeRecordQueryDTO();
        queryDTO.setPage(page);
        queryDTO.setSize(size);
        queryDTO.setUsername(username);
        if (minAmount != null) {
            queryDTO.setMinAmount(java.math.BigDecimal.valueOf(minAmount));
        }
        if (maxAmount != null) {
            queryDTO.setMaxAmount(java.math.BigDecimal.valueOf(maxAmount));
        }
        queryDTO.setRechargeType(rechargeType);
        queryDTO.setStatus(status);
        if (startTime != null) {
            queryDTO.setStartTime(java.time.LocalDateTime.parse(startTime, java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        if (endTime != null) {
            queryDTO.setEndTime(java.time.LocalDateTime.parse(endTime, java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        queryDTO.setOrderNo(orderNo);
        queryDTO.setOperatorName(operatorName);
        
        return rechargeService.getRechargeRecordPage(queryDTO);
    }
    
    @Autowired
    private AdminService adminService;

    /**
     * 搜索用户（用于充值时选择用户）
     */
    @GetMapping("/users/search")
    @RequirePermission("admin:user:search")
    public Result<PageResult<User>> searchUsers(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "20") int size) {

        System.out.println("收到用户搜索请求 - keyword: " + keyword + ", page: " + page + ", size: " + size);
        Result<PageResult<User>> result = adminService.searchUsersWithPagination(keyword, page, size);
        System.out.println("用户搜索结果 - 总数: " + (result.getData() != null ? result.getData().getTotal() : 0));
        return result;
    }
} 